Skip to content

Add validation for invalid field API names in getFirstValue#5

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/add-validation-for-field-api-name
Draft

Add validation for invalid field API names in getFirstValue#5
Copilot wants to merge 2 commits into
mainfrom
copilot/add-validation-for-field-api-name

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 3, 2026

getFirstValue silently returned null when given a non-existent fieldApiName, making it impossible to distinguish a missing field from a null field value.

Changes

  • Validation logic (CollectionGetFirstValue_Records.cls): Before calling firstRecord.get(), checks the field exists in the SObject's describe field map. Throws IllegalArgumentException with the invalid field name and SObject type if not found.
  • Tests (CollectionGetFirstValue_RecordsTest.cls): Added testGetFirstValue_InvalidFieldApiName verifying the exception type, message content (field name + SObject type), and that no other exception types are thrown.
  • README: Updated Behavior Notes to reflect the new exception behavior.
// Before: silently returned null for bad field names
// After: throws with a clear message
// IllegalArgumentException: Invalid field API name 'BadField__xyz' for SObject type 'Account'.
SObject firstRecord = req.records[0];
Schema.SObjectType sObjectType = firstRecord.getSObjectType();
Map<String, Schema.SObjectField> fieldMap = sObjectType.getDescribe().fields.getMap();

if (!fieldMap.containsKey(req.fieldApiName.toLowerCase())) {
    throw new IllegalArgumentException(
        'Invalid field API name \'' + req.fieldApiName + '\' for SObject type \'' + sObjectType.getDescribe().getName() + '\'.'
    );
}

Copilot AI linked an issue Apr 3, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add validation for invalid field API name in getFirstValue method Add validation for invalid field API names in getFirstValue Apr 3, 2026
Copilot AI requested a review from ethandunzer April 3, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No Validation for Invalid Field API Name

2 participants